﻿using ApprovalFlow_WebApi.Model;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using Utility;

namespace ApprovalFlow_WebApi.PlugIn
{
    /// <summary>
    /// 登录插件
    /// </summary>
    public static class LoginPlugIn
    {
        /// <summary>
        /// 声明http请求
        /// </summary>
        private static readonly HttpClient _httpClient = new HttpClient();

        /// <summary>
        /// 获取登录人员位置信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static async Task<IpApiResponse?> GetLoginPositionMsg(HttpContext? context)
        {
            IpApiResponse? ipApiResponse = new IpApiResponse();
            //string? result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            //try
            //{
            //    if (string.IsNullOrEmpty(result))
            //    {
            //        ipApiResponse.ip = context.Connection.RemoteIpAddress.ToString();
            //    }
            //    if (string.IsNullOrEmpty(result))
            //    {
            //        ipApiResponse.ip = context.Request.Host.ToString();
            //    }
            //    if (string.IsNullOrEmpty(result))
            //    {
            //        ipApiResponse.ip = "127.0.0.1";
            //    }
            //    ipApiResponse = await GetCoordinatesByIPAsync(ipApiResponse.ip);
            //}
            //catch (Exception ex)
            //{
            //    ExceptionHelper.ThrowBusinessException(string.Format("获取ip出错,错误信息:{0}", ex.Message));
            //}

            string result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(result))
            {
                result = context.Connection.RemoteIpAddress?.ToString();
            }
            if (string.IsNullOrEmpty(result))
            {
                result = context.Request.Host.ToString();
            }

            if (string.IsNullOrEmpty(result))
            {
                result = "127.0.0.1";
            }
            ipApiResponse.ip = result;
            ipApiResponse = await GetCoordinatesByIPAsync(ipApiResponse.ip);
            return ipApiResponse;
        }

        /// <summary>
        /// 根据ip获取位置信息
        /// </summary>
        /// <param name="ip">ip</param>
        /// <returns></returns>
        private static async Task<IpApiResponse?> GetCoordinatesByIPAsync(string ip)
        {
            IpApiResponse? ipApiResponse = null;
            try
            {
                var response = await _httpClient.GetAsync($"http://ip9.com.cn/get?ip={ip}");
                //var response = await _httpClient.GetAsync("http://ip9.com.cn/get?ip=117.139.233.229");
                response.EnsureSuccessStatusCode();
                var requestModel = await response.Content.ReadAsStringAsync();
                ipApiResponse = JsonConvert.DeserializeObject<IpApiResponse>(requestModel);
            }
            catch (HttpRequestException ex)
            {
                ExceptionHelper.ThrowBusinessException(string.Format("根据ip获取地理位置出错,错误信息:{0}", ex.Message));
            }
            ipApiResponse.ip = ip;
            return ipApiResponse;
        }
    }
}
